package com.example.demo.util;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.rmi.ServerException;
import java.util.HashMap;
import java.util.UUID;

/**
 * 短信认证工具
 */
public class SmsUtil {

    public static String sendSms(String phoneNumber) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shenzhen", "LTAI5tEtP9Pw7JpX3vDLFGah", "TBVnoYg3aciVIkHGS2MM1VjunglIIj");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "阳阳商城");
        request.putQueryParameter("TemplateCode", "SMS_205409285");
        //随机生成四位数
        String code = UUID.randomUUID().toString().substring(0,4);
        HashMap<String, String> map = new HashMap<>(1);
        map.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            if(response.getHttpResponse().isSuccess()){
                return code;
            }else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
